package cn.yr.controller;

import cn.yr.common.core.domain.entity.YrMessage;
import cn.yr.common.core.domain.vo.PageVo;
import cn.yr.common.exception.ret.Ret;
import cn.yr.service.YrMessageService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * 
 * @author Lang
 * @email 1102076808@qq.com
 * @date 2021-01-10 18:58:48
 */
@Api(tags = "即时通讯")
@Slf4j
@RestController
@RequestMapping("/im/message")
public class YrMessageController {

	@Autowired
    private YrMessageService yrMessageService;

	/**
     * 分页列表
     */
	@RequestMapping("/page")
	public Ret page(@RequestBody PageVo<YrMessage> pageVo){
		QueryWrapper<YrMessage> queryWrapper = new QueryWrapper<>();
		queryWrapper.orderByDesc("create_time");
		PageVo<YrMessage> pageYrMessage = yrMessageService.page(pageVo,queryWrapper);
		List<YrMessage> records = pageYrMessage.getRecords();
        ArrayList<YrMessage> sortMessages = new ArrayList<>(records.size());
        for (int i = records.size()-1; i >= 0; i--) {
            sortMessages.add(records.get(i));
        }
        pageYrMessage.setRecords(sortMessages);
		return Ret.success(pageYrMessage);
	}

    /**
     * 列表
     */
    @RequestMapping("/list")
    public Ret list(){
        return Ret.success(yrMessageService.list());
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public Ret info(@PathVariable("id") Integer id){
		YrMessage  yrMessage = yrMessageService.getById(id);
	    return Ret.success(yrMessage);
    }

    /**
     * 保存
     */
    @RequestMapping("/add")
    public Ret save(@Validated @RequestBody YrMessage yrMessage){
		yrMessageService.save(yrMessage);
	    return Ret.success();
    }

    /**
     * 修改
     */
    @RequestMapping("/edit")
    public Ret update(@Validated @RequestBody YrMessage yrMessage){
		yrMessageService.updateById(yrMessage);
	    return Ret.success();
    }

    /**
     * 删除
     */
    @RequestMapping("/remove/{ids}")
    public Ret delete(@PathVariable Integer[] ids){
		yrMessageService.removeByIds(Arrays.asList(ids));
        return Ret.success();
    }

}
